package net.wanglu.www.zzz.rule.sva;

import java.util.HashMap;
import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import net.wanglu.www.zzz.service.se.nsubj._NSubjTuple;
import edu.stanford.nlp.trees.TreeGraphNode;

public class EndWithSOnlyPAsSubj extends _SvaRule {

	public static final Pattern EndWithSOnlyP = Pattern.compile("\\baccommodations\\b|\\balms\\b|\\bannals\\b|\\bAntipodes\\b|\\barchives\\b|\\barticles\\b|\\bashes\\b|\\bauspices\\b|\\bbanns\\b|\\bbrains\\b|\\bcongratulations\\b|\\bCommons\\b|\\bLords\\b|\\bcontents\\b|\\bdregs\\b|\\bdues\\b|\\beaves\\b|\\bgoods\\b|\\bentrails\\b|\\bfunds\\b|\\bgrassroots\\b|\\bgreens\\b|\\bguts\\b|\\bheads\\b|\\bletters\\b|\\blooks\\b|\\bmanners\\b|\\bmeans\\b|\\bminutes\\b|\\boats\\b|\\bodds\\b|\\bpains\\b|\\boutskirts\\b|\\bparticulars\\b|\\bquarters\\b|\\bregards\\b|\\bremains\\b|\\briches\\b|\\bspirits\\b|\\bsports\\b|\\bstairs\\b|\\bsuburbs\\b|\\bsuds\\b|\\bthanks\\b|\\btroops\\b|\\btropics\\b|\\bvaluables\\b|\\bwages\\b|\\bwritings\\b", Pattern.CASE_INSENSITIVE);

	@Override
	public Object applicable(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		if ( super.applicable(objects) == Rule.Inapplicable) return Rule.Inapplicable;
		_NSubjTuple nst = (_NSubjTuple) objects[1];
		if ( !"NP".equals(nst.getSubjectAncestor().value())) return Rule.Inapplicable;
		return Rule.Applicable;
	}

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		_NSubjTuple nst =  (_NSubjTuple) objects[1];
		
		if ( this.applicable(preprocessData, nst) == Rule.Inapplicable) return Rule.Inapplicable;
		TreeGraphNode subj = (TreeGraphNode) nst.getSubj();
		TreeGraphNode verb = (TreeGraphNode) nst.getVerb();
		TreeGraphNode cop = (TreeGraphNode) nst.getCop();
		TreeGraphNode closestAux = (TreeGraphNode) nst.getAux();
		
		TreeGraphNode pred = verb;
		if ( cop != null) pred = cop;
		if ( closestAux != null) pred = closestAux;
		String predWord = pred.label().word();
		String predTag = pred.label().tag();

		if ( EndWithSOnlyPAsSubj.EndWithSOnlyP.matcher(subj.label().word()).find()) {
			if (Rule.PluralVerb.matcher(predTag).find() && !"was".equals(predWord)) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				egg.whereIsWrong = nst;
				return egg;
			}
		}
		return Rule.Inapplicable;
	}
	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb.append("英语中有一些名词只有复数形式，其后的谓语动词用复数。这类名词有：accommodations，alms，annals，Antipodes，archives，articles，ashes，auspices，banns，brains，congratulations，Commons，Lords，contents，dregs，dues，eaves，goods，entrails，funds，grassroots，greens，guts，heads，letters，looks，manners，means，minutes，oats，odds，pains，outskirts，particulars，quarters，regards，remains，riches，spirits，sports，stairs，suburbs，suds，thanks，troops，tropics，valuables，wages，whereabouts，writings等。");
		sb.append("<br>");
		sb.append("注意：这些名词中有一些既可以作单数用，又可做复数用。例如：odds，nerves，whereabouts，remains。");
		sb.append("<br>");
		sb.append("wages之后总是跟复数形式的动词，但在《圣经》中有接单数形式的动词。");
		sb.append("</p>");
		return sb.toString();
	}
}
